# TODO: Add comment
# 
# Author: Roger
###############################################################################


getLeapYearPeriod <- function(InterestStartDate,InterestEndDate){
	isLeap <- isLeapYearBetween(InterestStartDate,InterestEndDate)
	Result <- rep(365,length(isLeap))
	Result[isLeap] <- 366
	Result
}


dayCount30 <- function(InterestStartDate,InterestPayDate){
	# dayCount30(InterestStartDate = DateYMD(2009,1:2,28),InterestPayDate = DateYMD(2009,4,10))

	Y1 <- year(InterestStartDate)
	Y2 <- year(InterestPayDate)
	
	M1 <- month(InterestStartDate)
	M2 <- month(InterestPayDate)
	
	D1 <- day(InterestStartDate)
	D2 <- day(InterestPayDate)
	
	D1[D1==31] <- 30
	D1[isLastFebruary(InterestStartDate)] <- 30
	
	D2[D2==31] <- 30
	D2[isLastFebruary(InterestPayDate)] <- 30
	
	N <- (D2 - D1) + 30 * (M2 - M1) + 360 * (Y2 - Y1)
	N
}

dayCount30S <- function(InterestStartDate,InterestPayDate){
	# dayCount30S(InterestStartDate = DateYMD(2009,1:2,28),InterestPayDate = DateYMD(2009,4,10))
	
	Y1 <- year(InterestStartDate)
	Y2 <- year(InterestPayDate)
	
	M1 <- month(InterestStartDate)
	M2 <- month(InterestPayDate)
	
	D1 <- day(InterestStartDate)
	D2 <- day(InterestPayDate)
	
	D1[D1==31] <- 30
	D2[D2==31] <- 30
	
	N <- (D2 - D1) + 30 * (M2 - M1) + 360 * (Y2 - Y1)
	N
}

dayCountActual <- function(InterestStartDate,InterestPayDate){
	# dayCountActual(InterestStartDate,InterestPayDate)
	N <- as.numeric(InterestPayDate - InterestStartDate)
	N
}

dayCountActualNoLeap <- function(InterestStartDate,InterestPayDate){
	# dayCountActualNoLeap(InterestStartDate,InterestPayDate)
	N <- as.numeric(InterestPayDate - InterestStartDate)
	nLeapYears <- sapply((1:length(N)),function(i,InterestStartDate,N){
				sum(month(InterestStartDate[i]+1:N[i])==2 & day(InterestStartDate[i]+1:N[i])==29)
			},InterestStartDate,N)
	N - nLeapYears
}

dayCountActualISDA <- function(InterestStartDate,InterestPayDate){
	# InterestStartDate <- c(DateYMD(2003,2,28),DateYMD(2004,3,2))
	# InterestPayDate <- c(DateYMD(2004,3,2),DateYMD(2005,2,28))
	DateMatrix <- cbind(InterestStartDate+0,InterestPayDate-1)
	
	Result <- apply(DateMatrix,1,function(x){
				YearsInSequence <- year(as.Date(x[1]:x[2],origin="1970-01-01"))	
				LeapYearDays <- isLeapYear(YearsInSequence)
				c(sum(!LeapYearDays),sum(LeapYearDays))
			})
	Result
}

dayCount30U <- function(InterestStartDate,InterestPayDate){
	# dayCount30U(InterestStartDate = DateYMD(2009,1:2,28),InterestPayDate = DateYMD(2009,4,10))
	
	Y1 <- year(InterestStartDate)
	Y2 <- year(InterestPayDate)
	
	M1 <- month(InterestStartDate)
	M2 <- month(InterestPayDate)
	
	D1 <- day(InterestStartDate)
	D2 <- day(InterestPayDate)
	
	D2[isLastFebruary(InterestStartDate) & isLastFebruary(InterestPayDate)] <- 30
	D1[isLastFebruary(InterestStartDate)] <- 30

	D2[D2==31 & D1>=30] <- 30
	D1[D1==31] <- 30

	N <- (D2 - D1) + 30 * (M2 - M1) + 360 * (Y2 - Y1)
	N
}

periodCount360 <- function(Frequency){
	DaysInYear <- 360
	DaysInPeriod <- DaysInYear/Frequency
	DaysInPeriod
}

periodCount365 <- function(Frequency){
	DaysInYear <- 365
	DaysInPeriod <- DaysInYear/Frequency
	DaysInPeriod
}

periodCount365L <- function(Frequency,InterestStartDate,InterestEndDate){
	# InterestStartDate <- c(DateYMD(2003,2,28),DateYMD(2009,2,28))
	# InterestEndDate <- c(DateYMD(2004,3,1),DateYMD(2010,2,28))
	# Frequency <- 12
	# Test <- monthlyDateSequence(Sys.Date(),DateYMD(2060,11,30),12,Direction=-1)
	# Test2 <- getTradingDay(Test,"SWISSEXCHANGE")
	# InterestStartDate <- Test2[1:611]
	# InterestPayDate <- Test2[2:612]
	# InterestEndDate <- Test2[2:612]
	
	Y1 <- year(InterestStartDate)
	Y3 <- year(InterestEndDate)
	
	M1 <- month(InterestStartDate)
	M3 <- month(InterestEndDate)
	
	D1 <- day(InterestStartDate)
	D3 <- day(InterestEndDate)
	
	if(Frequency==1){
		DaysInYear <- as.numeric(InterestEndDate - InterestStartDate)
		notStandardYear <- !(DaysInYear==365 | DaysInYear==366)
		if(length(notStandardYear)>0){
			DaysInYear[notStandardYear] <- getLeapYearPeriod(InterestStartDate[notStandardYear],InterestEndDate[notStandardYear])
		}
	}
	else{
		DaysInYear <- rep(365,length(Y3))
		DaysInYear[isLeapYear(Y3)] <- 366
	}
	DaysInPeriod <- DaysInYear/Frequency
	DaysInPeriod
}

periodCountActualISDA <- function(Frequency){
	# Frequency = 2
	DaysInYear <- c(365,366)
	DaysInPeriod <- DaysInYear/Frequency
	DaysInPeriod
}

periodCountActualISMA <- function(Frequency,InterestStartDate){
	# Frequency <- 2
	# InterestStartDate <- Sys.Date()
	if(length(Frequency)!=length(InterestStartDate)){
		Frequency <- rep(Frequency,length(InterestStartDate))
	}
	InterestEndDate <- sapply((1:length(InterestStartDate)),function(i,InterestStartDate,Frequency){
				monthlyDateSequence(StartDate=InterestStartDate[i], EndDate=InterestStartDate[i]+1, Frequency[i], Direction = 1, overlapping = TRUE, keepEndOfMonth = FALSE)[2]
			},InterestStartDate,Frequency)
	InterestEndDate <- as.Date(InterestEndDate,origin="1970-01-01")
	
	DaysInPeriod <- as.numeric(InterestEndDate - InterestStartDate)
	DaysInPeriod
}

periodCountActualISMAUltimo <- function(Frequency,InterestStartDate){
	# Frequency <- 2
	# InterestStartDate <- c(DateYMD(2008,2,29),DateYMD(2008,8,31))
	if(length(Frequency)!=length(InterestStartDate)){
		Frequency <- rep(Frequency,length(InterestStartDate))
	}
	InterestEndDate <- sapply((1:length(InterestStartDate)),function(i,InterestStartDate,Frequency){
				monthlyDateSequence(StartDate=InterestStartDate[i], EndDate=InterestStartDate[i]+1, Frequency[i], Direction = 1, overlapping = TRUE, keepEndOfMonth = TRUE)[2]
			},InterestStartDate,Frequency)
	InterestEndDate <- as.Date(InterestEndDate,origin="1970-01-01")

	DaysInPeriod <- as.numeric(InterestEndDate - InterestStartDate)
	DaysInPeriod
}

accruedInterest <- function(Rate,DayCountConvention,Frequency,InterestStartDate,InterestPayDate,InterestEndDate){
	# DayCountConvention <- "ActualISDA/ActualISDA"
	# Frequency <- 2
	# tmp <- monthlyDateSequence(Sys.Date(),DateYMD(2020,11,30),2,Direction=-1)
	# InterestStartDate <- tmp[1:(length(tmp)-1)]
	# InterestPayDate <- tmp[2:(length(tmp))]
	# InterestEndDate <- tmp[2:(length(tmp))]
	
	# German 30/360 
	if(DayCountConvention=="30/360"){
		D <- dayCount30(InterestStartDate,InterestPayDate)
		P <- periodCount360(Frequency)
		F <- D/P
		AI <- Rate / Frequency * F
	}
	# US 30U/360
	if(DayCountConvention=="30U/360"){
		D <- dayCount30U(InterestStartDate,InterestPayDate)
		P <- periodCount360(Frequency)
		F <- D/P
		AI <- Rate / Frequency * F
	}
	# Special German 30S/360
	if(DayCountConvention=="30S/360"){
		D <- dayCount30S(InterestStartDate,InterestPayDate)
		P <- periodCount360(Frequency)
		F <- D/P
		AI <- Rate / Frequency * F
	}
	# Actual ISDA
	if(DayCountConvention=="ISDA:Act/Act"){
		D <- dayCountActualISDA(InterestStartDate,InterestPayDate)
		P <- periodCountActualISDA(Frequency)
		F <- colSums(D/P)
		AI <- Rate / Frequency * F
	}
	# Actual/365L
	if(DayCountConvention=="Actual/365L"){
		D <- dayCountActual(InterestStartDate,InterestPayDate)
		P <- periodCount365L(Frequency,InterestStartDate,InterestEndDate)
		F <- D/P
		AI <- Rate / Frequency * F
	}
	# Actual/365
	if(DayCountConvention=="Actual/365"){
		D <- dayCountActual(InterestStartDate,InterestPayDate)
		P <- periodCount365(Frequency)
		F <- D/P
		AI <- Rate / Frequency * F
	}
	# Actual/360
	if(DayCountConvention=="Actual/360"){
		D <- dayCountActual(InterestStartDate,InterestPayDate)
		P <- periodCount360(Frequency)
		F <- D/P
		AI <- Rate / Frequency * F
	}
	# ISMA: Act/Act
	if(DayCountConvention=="ISMA:Act/Act"){
		D <- dayCountActual(InterestStartDate,InterestPayDate)
		P <- periodCountActualISMA(Frequency,InterestStartDate)
		F <- D/P
		AI <- Rate / Frequency * F
	}
	# ISMA_Ultimo: Act/Act
	if(DayCountConvention=="ISMA_Ultimo:Act/Act"){
		D <- dayCountActual(InterestStartDate,InterestPayDate)
		P <- periodCountActualISMAUltimo(Frequency,InterestStartDate)
		F <- D/P
		AI <- Rate / Frequency * F
	}
	AI
}



#accruedInterestFactorOld <- function(Frequency,InterestStartDate,InterestEndDate,InterestPayDate,DayCountConvention){
	# Frequency = 2
	# InterestStartDate = DateYMD(2010,2,15)+0:1000
	# InterestEndDate = DateYMD(2011,2,15)+0:1000
	# InterestPayDate = DateYMD(2011,2,17)+0:1000
	# DayCountConvention = "30/360"
	# accruedInterestFactor(Frequency,InterestStartDate,InterestEndDate,InterestPayDate,DayCountConvention)
	
#	SplitConvention <- strsplit(DayCountConvention,"/")[[1]]
	
#	DayCountCommand <- paste("dayCount",SplitConvention[1],sep="")
#	PeriodCountCommand <- paste("periodCount",SplitConvention[2],sep="")
	
#	DayCountFunction <- match.fun(DayCountCommand)
#	PeriodCountFunction <- match.fun(PeriodCountCommand)
	
#	Nominator <- DayCountFunction(InterestStartDate,InterestPayDate)
#	Denominator <- PeriodCountFunction(Frequency,InterestStartDate,InterestEndDate)
	
#	Nominator/Denominator
#}



